package org.ouwa.lms.client.ui;

import org.ouwa.lms.client.manager.ManagerRegistry;
import org.ouwa.lms.client.support.event.ApplicationEvent;
import org.ouwa.lms.client.support.event.ApplicationEventListener;
import org.ouwa.lms.client.support.event.ApplicationEventListenerCollection;
import org.ouwa.lms.client.support.event.ApplicationEventSource;

import com.google.gwt.user.client.ui.DialogBox;

public abstract class EventDialogBox extends DialogBox implements
		ApplicationEventSource {
	private final ApplicationEventListenerCollection listeners;
	private final ManagerRegistry managerRegistry;
	
	/**
     * Constructs a new <code>DialogBox</code>.
     */
	protected EventDialogBox(ManagerRegistry managerRegistry) {
		super(false, true);
		this.managerRegistry = managerRegistry;
		listeners = new ApplicationEventListenerCollection();
	}
	
	/**
     * {@inheritDoc}
     */
	@Override
	public void addListener(ApplicationEventListener listener) {
		listeners.add(listener);
	}
	
	/**
     * {@inheritDoc}
     */
	@Override
	public void clearListeners() {
		listeners.clear();
	}
	
	/**
     * {@inheritDoc}
     */
	@Override
	public void removeListener(ApplicationEventListener listener) {
		listeners.remove(listener);
	}
	
	/**
     * Fires the given event to all the registered listener.
     *
     * @param event The event to fire.
     */
	protected void fireEvent(ApplicationEvent event) {
		listeners.fireEvent(event);
	}
	
	/**
     * Returns the manager registry associated with this dialog box.
     *
     * @return The manager registry associated with this dialog box.
     */
    protected ManagerRegistry getManagerRegistry() {
        return managerRegistry;
    }
}
